﻿@inject IHttpService _httpService

<MDCard Class="h-100 pa-4 mb-4" Outline>
        @if (isReady)
        {
            <div style=" display: flex; flex-direction: column; justify-content: space-between; height: 100%;">
                <div>
                    <MECharts Option="pie" Width="@("100%")" Height="450">
                    </MECharts>
                </div>
                <div>
                    <MECharts Option="bar" Width="@("100%")" Height="450">
                    </MECharts>
                </div>

            </div>
        }
</MDCard>


@code {
    [Parameter]
    public bool IsTransverse { get; set; }

    bool isReady = false;
    public PerfectionLevelOverviewModel Model { get; set; } = new PerfectionLevelOverviewModel();

    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }

    EChartsPieOptionModel pie = new EChartsPieOptionModel();
    EChartsOptionModel bar = new EChartsOptionModel();

    protected override async Task OnInitializedAsync()
    {
        try
        {
            Model = await _httpService.GetAsync<PerfectionLevelOverviewModel>("api/perfections/GetPerfectionLevelOverview");
            OnPieInit();
            OnBarInit();
            isReady = true;
            StateHasChanged();
        }
        catch(Exception ex)
        {
            await ErrorHandler.ProcessError(ex, "获取完善度概览失败");
        }
    }

    private void OnPieInit()
    {
        pie.Title.Text = "全站完善度概览";
        pie.Series.Add(new EChartsPieOptionSery
        {
            Name = "全站完善度概览",
            Data = new List<EChartsPieOptionSeryData>
                {
                    new EChartsPieOptionSeryData{Name="已完善",Value=Model.ExcellentCount},
                    new EChartsPieOptionSeryData{Name="待完善",Value=Model.GoodCount},
                    new EChartsPieOptionSeryData{Name="急需完善",Value=Model.ToBeImprovedCount},
                }
        });
    }

    private void OnBarInit()
    {
        bar.Title.Text = "统计数值";
        bar.XAxis.Data = new List<string>() { "平均数", "众数", "中位数", "标准差" };
        bar.Series.Add(new EChartsOptionSery
        {
            Type = "bar",
            Stack = "default",
            Data = new List<double> { Math.Round(Model.AverageValue, 2),Model.Mode, Math.Round(Model.Median, 2), Math.Round(Model.StandardDeviation, 2) }
        });
        
    }

}
